package com.pd.backend.service.systematic.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.pd.backend.common.PageResult;
import com.pd.backend.common.R;
import com.pd.backend.common.utils.DateUtils;
import com.pd.backend.common.utils.StringUtils;
import com.pd.backend.common.utils.page.PageUtils;
import com.pd.backend.common.utils.redis.RedisCache;
import com.pd.backend.domain.dto.AuthUserDto;
import com.pd.backend.domain.entity.DGlobalUser;
import com.pd.backend.domain.entity.PdAuthUser;
import com.pd.backend.ex.CustomerException;
import com.pd.backend.mapper.AuthUserMapper;
import com.pd.backend.service.systematic.AuthUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 用户管理业务层实现类
 */
@Service
public class AuthUserServiceImpl implements AuthUserService {

    @Autowired
    private AuthUserMapper authUserMapper;

    @Autowired
    private RedisCache redisCache;


    /**
     * 多条件分页查询
     *
     * @param size            每页数据条数
     * @param current         当前页
     * @param account         账号
     * @param orgId           组织Id
     * @param startCreateTime 创建开始时间
     * @param endCreateTime   创建结束时间
     * @return
     */
    @Override
    public PageResult<PdAuthUser> page(Integer size, Integer current, String account, String orgId, Date startCreateTime, Date endCreateTime) {
        // 1.处理参数
        if (!(size instanceof Integer)) {
            throw new CustomerException("参数类型异常");
        }
        if (!(current instanceof Integer)) {
            throw new CustomerException("参数类型异常");
        }
        if (Objects.isNull(size)) {
            throw new CustomerException("参数异常");
        }
        if (Objects.isNull(current)) {
            throw new CustomerException("参数异常");
        }
        if (size <= 0) {
            size = 10;
        }
        if (current <= 0) {
            current = 1;
        }
        if (StringUtils.isNotEmpty(account)) {
            account = "%" + account + "%";
        }
        // 开始分页
        Page<PdAuthUser> page = PageHelper.startPage(current, size);

        // 多条件分页查询
        List<PdAuthUser> dbList = authUserMapper.pageSearch(account, orgId, startCreateTime, endCreateTime);
//        List<PdAuthUser> list = authUserMapper.findAll();

        // 将数据中的密码进行隐藏
        dbList = dbList.stream().map(item -> {
            // 将密码置为空字符串
            item.setPassword("");
            return item;
        }).collect(Collectors.toList());
        // 将数据封装为PageResult
        return PageUtils.copyPage(page);
    }

    /**
     * 用户管理页面  修改用户
     *
     * @param authUserDto
     * @return
     */
    @Override
    public R modify(AuthUserDto authUserDto) {
        // 处理参数
        if (Objects.isNull(authUserDto)) {
            throw new CustomerException("参数异常");
        }

        // 设置操作的用户
        Object redisObj = redisCache.getCacheObject("login_user:");
        if (!(redisObj instanceof DGlobalUser)) {
            throw new CustomerException("redisObj转换异常");
        } else {
            Long id = ((DGlobalUser) redisObj).getId();
            if (id == null || id <= 0) {
                // 设置默认id
                id = 1L;
            }
            authUserDto.setUpdateUser(id);
        }
        // 设置修改用户时间
        authUserDto.setUpdateTime(DateUtils.getTime());
        // 实现业务
        int r = authUserMapper.modify(authUserDto);
        if (r <= 0) {
            throw new CustomerException("更新失败");
        }
        // 返回数据
        R result = new R();
        result.setCode(200);
        result.setMsg("修改用户信息成功");
        return result;
    }

    /**
     * 用户管理 删除用户
     *
     * @param id
     * @return
     */
    @Override
    public R remove(String id) {
        // 处理参数
        if (StringUtils.isEmpty(id)) {
            throw new CustomerException("id有误");
        }
        int i = authUserMapper.remove(id);
        if (i <= 0) {
            throw new CustomerException("删除失败");
        }
        R result = new R();
        result.setCode(200);
        result.setMsg("用户删除成功");
        return result;
    }
}
